package com.trdcore.zyzkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trdcore.zyzkt.mapper.ChapterMapper;
import com.trdcore.zyzkt.model.vod.Chapter;
import com.trdcore.zyzkt.model.vod.Video;
import com.trdcore.zyzkt.service.ChapterService;
import com.trdcore.zyzkt.service.VideoService;
import com.trdcore.zyzkt.vo.vod.ChapterVo;
import com.trdcore.zyzkt.vo.vod.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Luffy
 * @since 2024-03-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {


    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getTreeList(Long courseId) {
        List<ChapterVo> finalChapterList=new ArrayList<>();
        QueryWrapper<Chapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<Chapter> chapters = baseMapper.selectList(wrapper);
        LambdaQueryWrapper<Video> wrapperVideo=new LambdaQueryWrapper<>();
        wrapperVideo.eq(Video::getCourseId,courseId);
        List<Video> videoList = videoService.list(wrapperVideo);
        for (Chapter chapter : chapters) {
            //得到课程每个章节
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalChapterList.add(chapterVo);
            List<VideoVo> videoVoList=new ArrayList<>();
            for (Video video : videoList) {
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }
            }
        chapterVo.setChildren(videoVoList);
        }
        return finalChapterList;
    }

    @Override
    public void removeChapterByCourseId(Long id) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
